*===================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*                Processing Provider's Interview data
*===================================================================*

* Open Data
  use "$rawdatap2\deidentified_provider_survey_261114", clear

* Check number of clusters (80) and observations (448)
  duplicates report q04
  assert r(unique_value)==80
  assert r(N)==448

* Source
  gen     source = "Provider Survey"
  lab var source "Data source"

* Wave
  gen     wave = 2
  lab var wave "Wave"

* Sample Selection
  gen     sample1 = 0 
  replace sample1 = 1 if i02==1
  lab def sample1 0 "incomplete" 1 "complete"
  lab val sample1 sample1
  lab var sample1 "Sample Providers' interview"

* Provider ID
/*353937 should be 353037 
  94035  should be kac901. It is one of the oversampled WHP*/
  replace uid = 353037 if uid == 353937
  gen     prov_id = uid
  lab var prov_id "Provider ID"

* Location Variables
  gen     district = q02
  lab var district "District"
  gen     block = q03
  lab var block "Block"
  gen     cluster = q04
  lab var cluster "Cluster"
  gen     village = q05
  lab var village "Village"

* WHP-TPC Providers - OVERSAMPLED
  count
  gen UID = prov_id
  merge 1:1 UID using "$rawdatap2\DeidentifiedProviderListingData_270315", keepus(Provider_Type Provider_Location Cluster_number)
  drop if _merge==2

  gen     TPC = 0 if Provider_Type!=.
  replace TPC = 1 if Provider_Type==3
  lab var TPC "WHP-TPC provider"
  tab TPC, nol
  drop _merge UID Provider_Type Provider_Location

* WHP-RHP Providers - NON-OVERSAMPLED
  merge 1:1 uid using "$rawdatap2\specific_whp_providers_sample", keepus(sky_care_id_1) force

  gen     RHP = 0 if TPC!=. & _merge!=3
  replace RHP = 1 if _merge==3
  lab var RHP "WHP non-oversampled"
  drop _merge sky_care_id_1

* Fixing TPC identification and identifying oversampled
  list prov_id TPC RHP
  list prov_id TPC RHP if TPC==1 | RHP==1
  replace TPC = 0 if TPC==. & RHP==1
  replace TPC = 0 if TPC==1 & RHP==1

  merge 1:1 uid using "$rawdatap2\tpc_providers"
  replace TPC = 1 if _merge==3

  gen     WHP_oversampled = 0 
  replace WHP_oversampled = 1 if _merge==3
  lab var WHP_oversampled "=1 if oversampled WHP"
  drop _merge cluster_code- village_code

* WHP Providers - ALL
  gen     WHP = 0 if TPC!=.
  replace WHP = 1 if TPC==1 | RHP==1
  lab var WHP "World Health Partners (WHP) provider"

* Treatment Clusters
/*NOTE: I received a new file from Kimberly Babiarz (Sun 01/03/2015 06:09) Listing_treatment_cluster - 
  Kim comments: "I don't have the provider data, but I'm attaching a dataset 
  that contains the treatment clusters from the field-listing. The dataset has 
  only 1 variable (cluster) and 148 observations - so any cluster not in this 
  dataset is considered control (according to the field listing)" 
  
  NOTE: from special oversampled WHP providers we get that clusters 12, 94 and 429*/

  sort cluster
  merge m:1 cluster using "$rawdatap2\Listing_treatment_clusters"
  gen     treat = 1 if _merge==3
  replace treat = 0 if _merge==1
  replace treat = 1 if (cluster==12 | cluster==94 | cluster==429)
  lab var treat "=1 if treated"
  drop if _merge==2
  drop _merge
  egen tag =tag(cluster)
  tab treat if tag          /*30 control and 50 treatment clusters*/
  drop tag

* Interview related variables
  gen     int_status  = i02
  lab var int_status "status of interview"
  gen     reason = i03
  lab var reason "reason for refusing to give the interview"
  gen     tnvisit= i04
  lab var tnvisit "Total # of visits made to the health facility"

  gen     int_code = i05_code
  lab var int_code "Interviewer code"
  gen     check_code = i06_code
  lab var check_code "Scrutinizer code"
  gen     sup_code = i07_code
  lab var sup_code "Supervisor code"

* Interview date
  gen     int_date=mdy(i01_mm, i01_dd, i01_yyyy)
  format  int_date %d
  lab var int_date "Date of interview"

* Non-medical practitioners (all missing)
  drop a07a-a07g

* Label yes/no
  label define yesno 0 "no" 1 "yes"

* Check providers per cluster
  bys cluster sample: gen obs=_n if sample1==1
  sum obs if sample1==1
  tab cluster if obs>5 & sample1==1
  drop obs


***********************************************
* SOCIOECONOMICS CHARACTERISTICS
***********************************************

* Age
  gen     age = a06_2a
  lab var age "Provider age"

* Sex
  recode a06_2g (2=0), gen(sex)
  lab var sex "=1 if male"

* Experience
  gen     experience = a06_5
  lab var experience "Experience (years)"

* Medical Qualifications
  gen     educa_med = a06_3
  replace educa_med = 0 if a06_3==8
  replace educa_med = 8 if a06_3==88
  lab var educa_med "Highest Medical Qualification"
  #delimit ;
  lab def educa_med 
  0 "No degree"
  1 "mbbs"
  2 "bds (dentist)"
  3 "bams"
  4 "bums"
  5 "bhms/dhms"
  6 "md/ms/dnb/etc (allopathic)"
  7 "md/etc (non allopathic)"
  8 "Other";
  #delimit cr
  lab val educa_med educa_med

* Dummies Medical qualification
  gen     mbbs = inlist(a06_3, 1, 6) if a06_3!=.
  lab var mbbs "MBBS, MD/MS/DNB (allopathic)"

  gen     bams = inlist(a06_3, 3, 4, 5, 7) if a06_3!=.
  replace bams = 1 if regexm(a063_oth, "AYUVAIDIC CENTER")
  lab var bams "BAMS/BHMS/BUMS/NON-ALLOPATHIC M/Dip. in Ayurvedic"

  gen     semi = 0
  replace semi = 1 if regexm(a063_oth, "FARMA") 
  replace semi = 1 if regexm(a063_oth, "PHARMA") 
  lab var semi "Semi trained"

  gen     informal = inlist(a06_3, 2, 88) if a06_3!=.
  replace informal = 0 if (regexm(a063_oth, "AYUVAIDIC CENTER") | regexm(a063_oth, "FARMA") | regexm(a063_oth, "PHARMA") )
  lab var informal "Informal training"
  
  gen     none = inlist(a06_3, 8) if a06_3!=.
  lab var none "None"

* Medical/No Medical Qualification
  gen     mqualif = 0 if none==1 | informal==1 | semi==1
  replace mqualif = 1 if mbbs==1 | bams==1
  lab var mqualif "High/Low medical qualification"
  lab def mqualif 0 "No Medical Qualification" 1 "Medical Qualification"
  lab val mqualif mqualif

* Non-Medical Qualifications
  gen     educa_nomed = a06_4
  recode  educa_nomed (88=5)
  lab var educa_nomed "Higuest Non-medical Qualification"
  lab def educa_nomed 1 "<12th standard" 2 "12th standard pass" 3 "Graduate" 4 "Post-graduate" 5 "Any Other"
  lab val educa_nomed educa_nomed

  gen     high_school = inlist(a06_4, 2,3,4) if a06_4!=.
  lab var high_school "12 years or more of non-medical qualifications"


***********************************************
* MODULE A: WORKING CHARACTERISTICS
***********************************************

* Working Hours in facilities
  egen    work_hs_fac = rsum(a06_6 a06_7), missing
  lab var work_hs_fac "Total hs. per week he works in facilities"

* Working Hours visiting homes
  egen    work_hs_vis = rsum(a06_8 a06_9), missing
  lab var work_hs_vis "Total hs. per week he works visiting homes"

* Total woking hours
  egen    work_hs = rsum(work_hs_fac work_hs_vis), missing
  lab var work_hs "Total working hours per week"

* Task performed
  destring a0610_ot, replace
  foreach var of varlist a06_10_1-a0610_ot {
    tab `var', nol
    recode `var' (0=.) (88=8)
    }

  egen miss = rowmiss(a06_10_1-a0610_ot)
  forvalues n=1/8 { 
    egen    task_`n' =anymatch(a06_10_1-a0610_ot), v(`n')
    replace task_`n' =. if miss==9
    }

  lab var task_1 "=1 if consultation with patient"
  lab var task_2 "=1 if administering treatments"
  lab var task_3 "=1 if selling drugs"
  lab var task_4 "=1 if laboratory related duties"
  lab var task_5 "=1 if administrative duties"
  lab var task_6 "=1 if cleaning duties"
  lab var task_7 "=1 if ownership"
  lab var task_8 "=1 if others"
  drop miss


* Type of diseases treated
  foreach var of varlist a08_1-a08_7 {
    recode `var' (0=.) (88=7)
    }

  egen miss = rowmiss(a08_1-a08_7)
  forvalues n=1/7 {
    egen    disease_`n'=anymatch(a08_1-a08_7), v(`n')
    replace disease_`n'=. if miss==7
    }

  lab var disease_1 "Fever"
  lab var disease_2 "Cough"
  lab var disease_3 "Diarrhea"
  lab var disease_4 "Pneumonia"
  lab var disease_5 "Tubercosis"
  lab var disease_6 "VL"
  lab var disease_7 "Others"
  drop miss

* Run camps?
/*a09: does this facility run camps*/
  recode a09 (2=0), gen(camps)
  lab var camps "=1 if facility run camps"
  lab val camps yesno

* Last time camp was run
  foreach var of varlist a10a_mm-a10c_yyy {
    recode `var' 0 = .
    }
  egen    last_camp=rmax(a10a_yyy a10b_yyy a10c_yyy)
  lab var last_camp "last time camp was run (year)"

* Partnership/Agreement with health care org.
  recode a11 (2=0), gen(health_part)
  lab var health_part "=1 if partnership with health organization"
  lab val health_part yesno

* Desire of Partnership/Agreement
  recode a12 (2=0), gen(part_desire)
  lab var part_desire "=1 if a partnership is desire"
  lab val part_desire yesno

* Training
  recode a13 (2=0), gen(training)
  lab var training "=1 if someone in facility received training"
  lab val training yesno


***********************************************
* MODULE B and D - FACILITY CHARACTERISTICS
***********************************************
* Facility ownership
  gen     fac_rent=(b02==1) if b02!=.
  lab var fac_rent "=1 if facility is rented"

  gen     fac_owner=(b02==2) if b02!=.
  lab var fac_owner "=1 if ownership of facility"

  gen     fac_free=(b02==3) if b02!=.
  lab var fac_free "=1 if facility is free"

* Owners medical facilities
  gen     fac_num=0 if b01==4
  replace fac_num=1 if b01>=1 & b01<=3
  lab var fac_num "=1 if owner run 2 or more facilities"

* Facility as part of a chain of clinics
  recode b03 (2=0), gen(fac_chain)
  lab var fac_chain "=1 if facility is part of a chain of clinics"
  lab val fac_chain yesno

* Electricity
  recode d01 (2=0), gen(electricity)
  lab var electricity "=1 if facility has electricity"
  lab val electricity yesno

* Power cut frequency 
  recode d02 (2 3 = 0), gen (power_cut)
  lab var power_cut "=1 if experience power cut more than once a day"

* Power cut duration - last time
  gen     powercut_dur = d03_hh + d03_mm/60
  lab var powercut_dur "duration of last time power cut (hours)"

* Has power backup
  recode d04 (2=0), gen(power_backup)
  lab var power_backup "=1 if has power backup"

* Type of medicine practiced
  foreach var of varlist b04_1-b04_6 {
    recode `var' (0=.) (8=6)
    }

  egen miss=rowmiss(b04_1-b04_6)
  forvalues n=1/6 {
    egen    treatment_type_`n'=anymatch(b04_1-b04_6), v(`n')
    replace treatment_type_`n'=. if miss==6
    }

  lab var treatment_type_1 "Allopathic"
  lab var treatment_type_2 "Homeopathic"
  lab var treatment_type_3 "Ayurvedic"
  lab var treatment_type_4 "Unani"
  lab var treatment_type_5 "Siddha"
  lab var treatment_type_6 "Others"
  drop miss

* Facility ownership (sector)
  gen     fac_gov = (b05==1) if b05!=.
  lab var fac_gov "=1 if facility is from government"
  lab val fac_gov yesno

* Age of facility
  la var b06_mm   "b06 date of inception of the facility (month)"
  la var b06_yyyy "b06 date of inception of the facility (year)"

  gen    interview=ym(i01_yyyy, i01_mm)
  format interview %tm
  gen    fac_birth=ym(b06_yyyy, b06_mm)
  format fac_birth %tm
  gen     fac_age = (interview-fac_birth)/12
  replace fac_age = (i01_yyyy-b06_yyyy) if fac_age==. & b06_yyyy!=.
  lab var fac_age "Age of facility in years"
  drop interview fac_birth

* Number of consulting rooms
  gen     room_num = b07
  lab var room_num "Number of consulting rooms in facility"

* Night-care facility?
  recode b09 (2=0), gen(fac_night)
  lab var fac_night "=1 if night care facility"
  lab val fac_night yesno

* Number of beds for day care/overnight stay
  gen     bed_day = b08
  lab var bed_day "Number of Day Beds"

  gen     bed_night = b10
  lab var bed_night "Number of beds for overnight stay"

* Have X-ray?
  recode b11 (2=0), gen(x_ray)
  lab var x_ray "=1 if facility has x ray"
  lab val x_ray yesno

* Have laboratory testing provisions?
  recode b12 (2=0), gen(lab_test)
  lab var lab_test "=1 if having laboratory testing provitions"
  lab val lab_test yesno

* Provide Any test?
  foreach var of varlist b13_1-b13_9 {
    recode `var' (0=.) (88=9)
    }

  egen    test=rowmiss(b13_1-b13_9)
  replace test=1 if test<9
  replace test=0 if test==9
  lab var test "=1 if facility provide any test"
  lab val test yesno

* Type of test provided
  egen miss=rowmiss(b13_1-b13_9)
  forvalues n=1/9 {
    egen test_`n'=anymatch(b13_1-b13_9), v(`n')
    replace test_`n'=. if miss==9
    }

  lab var test_1 "Blood test"
  lab var test_2 "Haemoglobin test"
  lab var test_3 "TLC test"
  lab var test_4 "DLC test"
  lab var test_5 "Urime examination"
  lab var test_6 "Stool microscope examination"
  lab var test_7 "Sputum exam. for tuberculosis"
  lab var test_8 "Rapid VL test"
  lab var test_9 "Other test"
  drop miss

***********************************************
* MODULE C - MARKET
***********************************************
* Number of village patients come from (apart from the one where facility is located?)

  forvalues n=1/5 {
    replace c01_`n' = "" if regexm(c01_`n',"0")
    replace c01_`n' = "" if regexm(c01_`n',"00")
    replace c01_`n' = "" if c01_`n'== "."
    }

  egen    village_patient=rownonmiss(c01_1-c01_5), strok
  lab var village_patient "Number of villages from which patients have come"

* Distances
  gen     dist1 = c02
  lab var dist1 "Distance to the Nearest HCP (Kms)"
  gen     dist2 = c03
  lab var dist2 "Distance to the 2nd nearest HCP (Kms)"
  gen     dist3 = c04
  lab var dist3 "Distance to the 3rd nearest HCP (Kms)"
  gen     dist4 = c05
  lab var dist4 "Distance from where provider get the most patients (Kms)"

* Patient Volume
  gen     pat_winter = c06
  lab var pat_winter "Number of patients attended per day in winter"
  gen     pat_summer = c07
  lab var pat_summer "Number of patients attended per day in summer"
  gen     pat_pmoonson = c08
  lab var pat_pmoonson "Number of patients attended post-moonson"
  gen     pat_yesterday = c09
  lab var pat_yesterday "Number of patients attended yesterday"

  egen    npatient=rmean(pat_winter pat_summer pat_pmoonson)
  lab var npatient "Average caseload per day all seasons"


***********************************************
* MODULE E - PRICING
***********************************************
* Consultation Fee
  gen     c_fee = e01
  lab var c_fee "First consultation fee (excluding lab tests)"

* Percentage of Consultation fee that covers basic medicines (%)
  gen     prc_cfee = .
  lab var prc_cfee "% of fee that covers basic medicines"

* Follow-up Consultation Fee
  gen     fu_fee = e03
  lab var fu_fee "Follow-up consultation fee"
  
* Percentage of Follow-up Consultation fee that covers basic medicines
  gen     prc_fufee = .
  lab var prc_fufee "% of follow-up fee that covers basic medicines"

* Fee - different scenarios
  gen     fee_1 = e19 if e19!=9999
  lab var fee_1 "fee for examining a sick boy and spend 3 mim"

  gen     fee_2 = e20 if e20!=9999
  lab var fee_2 "3 mim consultation + 2 sachets of ors"

  gen     fee_3 = e21 if e21!=9999 & e21!=8888 & e21!=999
  lab var fee_3 "3 mim consultation + one bottle 30 ml of amoxycillin"

  gen     fee_4 = e22 if e22!=9999 & e22!=8888 & e22!=999
  lab var fee_4 "3 mim consultation + two bottle 30 ml of amoxycillin"

  gen     fee_5 = e23 if e23!=9999 & e23!=999
  lab var fee_5 "3 mim consultation + no medicine"

* Medicines prices
  rename e14 e16b
  rename e12 e13b
  rename e11 e12
  rename e09 e17b
  rename e08 e09
  rename e07 e08
  rename e06 e07
  gen e06=.
  gen e11=.
  gen e14=.

  local numlist "05 06 07 08 09 10 11 12 13b 14 16b 17b" 
  local i=1
  foreach n of local numlist {
    gen price_`i'=e`n' if (e`n'!=888 & e`n'!=999 & e`n'!=8888 & e`n'!=9999 & e`n'!=9998)
    local i = `i'+1
      }

  lab var price_1 "ORS packet"
  lab var price_2 "Sodium Stibo Gluconate (100 mg/ml vial)"
  lab var price_3 "Paracetamol 5 mg"
  lab var price_4 "Septran Syrup (40 mg/5 ml of trimethoprim)"
  lab var price_5 "Amoxycilin Dispersible Tablet (250 mg)"
  lab var price_6 "Antibiotic Tablet for diarrhea"
  lab var price_7 "Sodium Antimony Gluconate (100 mg/ml per vial)"
  lab var price_8 "Amphotericin-b (100 mg/ml per vial)"
  lab var price_9 "Ambisome (100 mg/ml per vial)"
  lab var price_10 "Red box for TB drugs (category 1)"
  lab var price_11 "Zinc Sulphate Tablet (20 mg)"
  lab var price_12 "30 ml of amoxycillin oral suspension (125 mg per 5 ml)"
  drop  e06 e11 e14

* How charge the fee
  egen miss=rowmiss(e15_1-e15_3)
  egen    charge_method_1 = anymatch(e15_1-e15_3), v(1)
  replace charge_method_1 =. if miss==3
  lab var charge_method_1 "Cash"

  egen    charge_method_2 = anymatch(e15_1-e15_3), v(2)
  replace charge_method_2 =. if miss==3
  lab var charge_method_2 "Credit"

  egen    charge_method_3 = anymatch(e15_1-e15_3), v(3)
  replace charge_method_3 =. if miss==3
  lab var charge_method_3 "Barter"

  egen    charge_method_4 = rsum(charge_method_1-charge_method_3)
  replace charge_method_4 =. if miss==3
  recode charge_method_4 (1=0) (2/3=1)
  lab var charge_method_4 "Combination of the above"
  drop miss

* Patient Socioeconomic Class
  gen     patient_class=.
  lab var patient_class "Patient Socioeconomic class"

* Difference in fee according to Socioeconomic class
  recode e18 (2=0), gen(diff_fee)
  lab var diff_fee "=1 if provider charges different fee according to socioec. class"


***********************************************
* Familiarity with IT
***********************************************
* Own a computer
  recode f03 (2=0), gen(comp_owner)
  lab var comp_owner "=1 if own a computer"

* Ever use computer
  recode f01 (2=0), gen(comp_ever)
  lab var comp_ever "Have ever used a computer"
  lab val comp_ever yesno 

* Frequency of Usage of Computer
  gen     comp_freq=0
  replace comp_freq=1 if f02<=3 & comp_ever==1
  lab var comp_freq "=1 if use computer at least once a week"
  
* Computer training
  recode f04 (2=0), gen(comp_training)
  lab var comp_training "=1 if has received computer training"

* Training intensity
  gen     comp_train_hs = f05
  lab var comp_train_hs "Computer training intensity (hours)"

* Computer in the facility
  recode f07 (2=0), gen(fac_comp)
  lab var fac_comp "=1 if facility has a computer"

* Computer to keep patient records
  recode f08 (2=0) if fac_comp==1, gen(fac_record)
  lab var fac_record "=1 if computerized system for patient record"


***********************************************
* Investment Attitudes
***********************************************
* Risk Aversion
  gen     risk=1 if g01a==2
  replace risk=2 if g01a==1 & g01b==2
  replace risk=3 if g01a==1 & g01b==1 & g01c==2
  replace risk=4 if g01a==1 & g01b==1 & g01c==1
  lab def risk 1 "Risk Lover" 2 "Medium-low risk averse" 3 "Medium-high risk averse" 4 "High Risk averse"
  lab val risk risk
  tab risk, gen(risk_)
  drop risk

* Discount Factor
  gen     patient=4 if g02a==2
  replace patient=3 if g02a==1 & g02b==2
  replace patient=2 if g02a==1 & g02b==1 & g02c==2
  replace patient=1 if g02a==1 & g02b==1 & g02c==1
  lab define pat 4 "High patient" 3 "Medium-high patient" 2 "Medium-low patient" 1 "Impatient"  
  lab val patient pat
  tab patient, gen(patient_)
  drop patient

***********************************************
* Assets/Infraestructure Index
***********************************************
* Correlation matrix
  corr electricity power_backup room_num bed_day test x_ray fac_comp
  sum  electricity power_backup room_num bed_day test x_ray fac_comp

* Infraestructure index
  pca electricity power_backup room_num bed_day test x_ray fac_comp
  predict iindex, score
  lab var iindex "infraestructure index"

* Infraestructure index (normalized)
  su iindex, meanonly 
  gen niindex = (iindex - r(min)) / (r(max) - r(min))


***********************************************
* Correcting District code to match with Baseline
***********************************************
  rename district district_or
  recode district_or (2=3) (3=5) (4=7) (5=4) (6=10) (7=8) (8=6) (9=11) (10=2) (11=9), gen(district)
  order district, after(district_or)

***********************************************
* Changes in cluster 387
***********************************************

replace block = 134 if cluster==387 & block==142
replace cluster = 462 if cluster ==387

***********************************************
* Saving processed data
***********************************************
  gen last=.
  keep source-last
  drop last
  label data "Provider Interview"
  sort prov_id
  compress
  save "$prodata2\providers_interview_2nd", replace
